package com.zkt.auth.application.controller;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.auth.application.convert.AuthRolePermissionDTOConverter;
import com.zkt.auth.application.dto.AuthRolePermissionDTO;
import com.zkt.auth.common.entity.Result;
import com.zkt.auth.domain.entity.AuthRolePermissionBO;
import com.zkt.auth.domain.service.AuthRolePermissionDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/22 15:52
 * @description 角色权限关系相关controller层
 **/

@RestController
@RequestMapping("/rolePermission")
@Slf4j
public class RolePermissionController {
	
	@Resource
	private AuthRolePermissionDomainService authRolePermissionDomainService;
	
	/**
	 * 新增角色权限关系
	 *
	 * @param authRolePermissionDTO 角色权限关系信息
	 * @return 是否成功
	 */
	@PostMapping("add")
	public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("RolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
			}
			
			if (ObjUtil.isEmpty(authRolePermissionDTO.getRoleId())) {
				throw new Exception("角色id不能为空");
			}
			if (ObjUtil.isEmpty(authRolePermissionDTO.getPermissionIdList())) {
				throw new Exception("权限id不能为空");
			}
			
			AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDtoToBo(authRolePermissionDTO);
			
			return Result.ok(authRolePermissionDomainService.add(authRolePermissionBO));
		} catch (Exception e) {
			log.error("RolePermissionController.add.error:{}", e.getMessage(), e);
			return Result.fail("新增角色权限关系失败");
		}
	}
	
}
